<template>
	<view>
		<image src="../../static/login/top.png" mode="widthFix" class="top"></image>
		<image src="../../static/login/logo.png" mode="widthFix" class="logo"></image>
		<view class="desc">
			<text class="name">（司机端）</text>
			<!-- <text class="version">Ver1.0.0</text> -->
		</view>
		<button class="btn" open-type="getPhoneNumber" @getphonenumber="login">微信登录</button>
		<view class="register-container">
			没有账号?
			<text class="link" @tap="toRegisterPage()">立即注册</text>
		</view>
		<text class="remark">本小程序仅限于嘿代驾签约司机使用，新司机必须先注册，经由实名认证之后方可在本小程序中接单运营。</text>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		return {};
	},
	methods: {
		async login(e) {
			let that = this;

			// console.log(e.detail.code)
			let phoneCode = e.detail.code;

			//获取微信登录的code
			let wxLoginCode = await this.getWxLoginCode();

			//做登录请求
			this.doLoginAction({ code: wxLoginCode, phoneCode: phoneCode });
		},

		//获取微信登录信息
		getWxLoginCode() {
			return new Promise((resolve, reject) => {
				uni.login({
					success(resp) {
						let code = resp.code;
						resolve(code);
					}
				});
			});
		},

		//登录操作
		doLoginAction(param) {
			let that = this;
			that.ajax(that.url.login, 'POST', param, function(resp) {
				if (!resp.data.hasOwnProperty('token')) {
					that.$refs.uToast.show({
						title: '请先注册',
						type: 'error'
					});
				} else {
					let token = resp.data.token;
					let realAuth = resp.data.realAuth;
					let archive = resp.data.archive;
					//本地存储手机号
					let tel = resp.data.tel;
					uni.setStorageSync('contact_tel', tel);
					uni.setStorageSync('token', token);
					uni.setStorageSync('realAuth', realAuth);
					uni.removeStorageSync('executeOrder');
					that.$refs.uToast.show({
						title: '登录成功',
						type: 'success',
						callback: () => {
							uni.setStorageSync('workStatus', '停止接单');
							//检查用户是否没有填写实名信息
							if (realAuth == 1) {
								uni.redirectTo({
									url: '../../identity/filling/filling?mode=create'
								});
							} else if (archive == false) {
								//检查系统是否存有司机的面部数据
								uni.showModal({
									title: '提示消息',
									content: '您还没有录入用于核实身份的面部特征信息，如果不录入将无法接单',
									confirmText: '录入',
									cancelText: '取消',
									success: resp => {
										if (resp.confirm) {
											//跳转到面部识别页面，采集人脸数据
											uni.redirectTo({
												url: '../../identity/face_camera/face_camera?mode=create'
											});
										} else {
											uni.switchTab({
												url: '../workbench/workbench'
											});
										}
									}
								});
							} else {
								uni.switchTab({
									url: '../workbench/workbench'
								});
							}
						}
					});
				}
			});
		},

		toRegisterPage: function() {
			uni.navigateTo({
				url: '../register/register'
			});
		}
	},

	onLoad() {
		//如果用户登录过,则直接进入workbench界面
		let token = uni.getStorageSync('token');
		// console.log(`token的值是${token}`);
		if (token) {
			//已经登录过
			let realAuth = uni.getStorageSync('realAuth');
			// let archive =uni.getStorageInfoSync('archive')
			if (realAuth == 1) {
				//判断是否注册过资料
				uni.redirectTo({
					url: '../../identity/filling/filling?mode=create'
				});
			} 
			// else if (archive == false) {
			// 	//判断是否录入了人
			// 	//检查系统是否存有司机的面部数据
			// 	//跳转到面部识别页面，采集人脸数据
			// 	uni.redirectTo({
			// 		url: '../../identity/face_camera/face_camera?mode=create'
			// 	});
			// } 
			else {
				//跳转到工作台界面
				uni.switchTab({
					url: '../workbench/workbench'
				});
			}
		}
	}
};
</script>

<style lang="less">
@import url('login.less');
</style>
